<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='https://cdn.staticfile.org/react/16.4.0/umd/react.development.js'></script>
    <script src='https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js'></script>
    <!-- 生产环境中不建议使用 -->
    <script src='https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js'></script>
<!-- PropTypes文件 -->
<script src='https://unpkg.com/prop-types@15.6.2/prop-types.js'></script>
</head>

<body>
    <div id='app'>
    </div>
    <script type='text/babel'>
        class Child extends React.Component{
            /*
                当父组件因为setState触发了render，子组件中的流程：
                componentWillReceiveProps、shouldComponentUpdate、componentWillUpdate、render、componentDidUpdate
            */
            // 子组件将要接收props的时候调用的钩子
            componentWillReceiveProps(){
                console.log('componentWillReceiveProps')
            }

            // 子组件是否可以更新
            shouldComponentUpdate(){
                console.log('shouldComponentUpdate');
                return true
            }

            // 子组件将要更新
            componentWillUpdate(){
                console.log('componentWillUpdate');
            }

            render(){
                console.log('render');
                return (
                    <div>车名是:{this.props.carName}</div>
                )
            }

            // 子组件更新完毕
            componentDidUpdate(){
                console.log('componentDidUpdate');
            }
        }

        class Parent extends React.Component{
            state = {carName:'Benz'}

            changeCar = ()=>{
                this.setState({carName:'Auto'})
            }

            render(){
                return (
                    <div>
                        <Child carName={this.state.carName}/>    
                        <button onClick={this.changeCar}>切换车名</button>
                    </div>
                )
            }
        }

        ReactDOM.render(<Parent/>,document.getElementById('app'))
    </script>
</body>

</html>